package com.xyauto.theme.fragment

import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xyauto.theme.R
import com.xyauto.theme.bean.IThemeBean
import com.xyauto.theme.utils.AutoUtils
import com.xyauto.theme.utils.getFileNameByUrl
import com.xyauto.theme.utils.setImageUrl
import com.xyauto.theme.utils.setSelected

class ThemeListAdapter : BaseQuickAdapter<IThemeBean, BaseViewHolder>(R.layout.item_main_list) {


    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        return super.onCreateDefViewHolder(parent, viewType).apply {
            val view = itemView
            if (view is ConstraintLayout) {
                val constraintSet = ConstraintSet()
                constraintSet.clone(view)
                constraintSet.setDimensionRatio(R.id.m_item_select_viw, AutoUtils.getPreviewScale())
                constraintSet.applyTo(view)
            }
            AutoUtils.auto(view)
        }
    }

    override fun convert(holder: BaseViewHolder, item: IThemeBean) {
        val selected = item.isSelected()
        LogUtils.i(getFileNameByUrl(item.getPreviewImage()), item.getPreviewImage())
        holder.setImageUrl(R.id.m_item_icon, item.getPreviewImage())
            .setSelected(R.id.m_item_select_viw, selected)
            .setText(R.id.m_item_text, item.getName())
    }
}